"""
老师视图层
"""

from lib import common
from interface import teacher_interface, common_interface

user_dict = {
    'user': None
}


# 老师登录函数
def login():
    while True:
        # 1. 接受用户输入
        username = input("请输入用户名: ").strip()
        password = input("请输入密码: ").strip()

        # 2. 校验用户输入
        if username == "" or password == "":
            print("用户名或密码不能为空！")
            continue

        # 3. 调用接口函数
        flag, msg = common_interface.login_common_interface(username, password, user_type='teacher')

        # 4. 显示返回结果
        if flag:
            print(msg)
            user_dict["user"] = username
            break
        else:
            print(msg)


# 老师查看教授课程函数
@common.auth('teacher')
def check_teach_course():
    msg = teacher_interface.check_teach_course_interface(user_dict["user"])
    if msg:
        for key,value in enumerate(msg):
            print(f"{key}: {value}")
    else:
        print("当前还没有课程！")


# 老师选择教授课程函数函数
@common.auth('teacher')
def select_teach_course():
    while True:
        # 1. 打印出所有的课程
        flag, msg = common_interface.get_all_course_interface()

        # 2. 如果没有课程则退出
        if not flag:
            print(msg)
            break

        # 3. 如果有学校则打印并让用户选择
        print("当前系统中的课程有: ")
        for key, value in enumerate(msg):
            print(f"\t{key}.{value}")
        choice = input("请输入编号选择要添加的学校: ").strip()

        if not choice.isdigit():
            print("必须输入数字")
            continue

        choice = int(choice)
        select_course = msg[choice]

        # 4. 调用接口函数
        msg = teacher_interface.select_teach_course_interface(
            select_course, user_dict["user"]
        )

        # 5. 显示返回结果
        print(msg)
        break


# 老师查看课程下学生函数
@common.auth('teacher')
def check_stu_of_course():
    while True:
        # 1. 输出老师的所有课程
        course_list = teacher_interface.check_teach_course_interface(user_dict.get('user'))

        # 2. 如果没有课程则退出
        if not course_list:
            print("当前还没有选择教授课程！")
            break

        # 3. 如果有课程则选择课程
        for key,value in enumerate(course_list):
            print(f"\t{key}.{value}")
            
        choice = input("请输入编号选择课程: ").strip()

        if not choice.isdigit():
            print("必须输入数字")
            continue

        choice = int(choice)
        select_course = course_list[choice]

        # 4. 调用接口函数
        student_list = teacher_interface.check_stu_of_course_interface(
            select_course, user_dict.get('user')
        )

        # 5. 显示返回结果
        print(student_list)
        break


# 老师修改学生成绩函数
@common.auth('teacher')
def modify_stu_score():
    while True:
        # 1. 输出老师的所有课程
        course_list = teacher_interface.check_teach_course_interface(user_dict.get('user'))

        # 2. 如果没有课程则退出
        if not course_list:
            print("当前还没有选择教授课程！")
            break

        # 3. 如果有课程则选择课程
        for key,value in enumerate(course_list):
            print(f"\t{key}.{value}")
            
        choice = input("请输入编号选择课程: ").strip()

        if not choice.isdigit():
            print("必须输入数字")
            continue

        choice = int(choice)
        select_course = course_list[choice]

        # 4. 调用接口函数
        student_list = teacher_interface.check_stu_of_course_interface(
            select_course, user_dict.get('user')
        )

        # 5. 显示返回结果
        for key,value in enumerate(student_list):
            print(f"\t{key}.{value}")
        
        choice = input("请输入编号选择学生: ").strip()

        if not choice.isdigit():
            print("必须输入数字")
            continue

        choice = int(choice)
        select_student = student_list[choice]

        score = input("请输入要修改的成绩: ")
        if not score.isdigit():
            print("成绩必须为数字")
            continue

        # 6. 调用接口函数
        msg = teacher_interface.modify_stu_score_interface(
            select_course, select_student, score, user_dict.get('user')
        )

        # 7. 显示结果
        print(msg)
        break


# 功能字典: 菜单，函数
func_dict = {
    "1": ("登录", login),
    "2": ("查看教授课程", check_teach_course),
    "3": ("选择教授课程", select_teach_course),
    "4": ("查看课程下学生", check_stu_of_course),
    "5": ("修改学生分数", modify_stu_score)
}


def teacher_view():
    while True:
        print("")
        print(" 欢迎来到老师界面 ".center(30, '='))
        for key,value in func_dict.items():
            print(f"\t    {key}.{value[0]}")
        print(" end ".center(38, '='))
        print("")

        choice = input("请输入功能编码(q退出): ").strip()

        if choice == 'q':
            break

        if choice not in func_dict:
            print("输入的值超出范围！")
            continue

        func_dict[choice][1]()